package cn.yunhe.dao.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.yunhe.dao.Userdao;
import cn.yunhe.entity.User;

public class UserDaoImpl {
	String resource="Config.xml";
	InputStream inputstream;
	SqlSessionFactory ssf;
	public List<User> findUserByName(String name) throws IOException  {
		inputstream=Resources.getResourceAsStream(resource);
		ssf=new SqlSessionFactoryBuilder().build(inputstream);
		SqlSession sqlsession=ssf.openSession();
		Userdao userdao=sqlsession.getMapper(Userdao.class);
		List<User> userlist=userdao.findUserByName(name);
		return userlist;
	}

	public User findUser(String name,String pwd) throws IOException{
		inputstream = Resources.getResourceAsStream(resource);
		ssf=new SqlSessionFactoryBuilder().build(inputstream);
		SqlSession sqlsession=ssf.openSession();
		Userdao userdao=sqlsession.getMapper(Userdao.class);
		User user=userdao.findUser(name,pwd);
		sqlsession.close();
		return user;
		
	}
	public void insertUser(User user) throws IOException {
		inputstream=Resources.getResourceAsStream(resource);
		ssf=new SqlSessionFactoryBuilder().build(inputstream);
		SqlSession sqlsession=ssf.openSession();
		Userdao userdao=sqlsession.getMapper(Userdao.class);
		userdao.insertUser(user);
		sqlsession.commit();
		sqlsession.close();
	}
	
}

